package com.ai.module.order.controller;

import com.ai.module.chat.common.ano.NoAuth;
import com.ai.module.chat.common.ano.NoLog;
import com.ai.module.chat.common.constant.RedisKeyStringConstant;
import com.ai.module.chat.util.DateUtils;
import com.ai.module.chat.util.RedisTemplateUtils;
import com.ai.module.order.domain.entity.WxOrderEntity;
import com.ai.module.order.domain.req.OrderReq;
import com.ai.module.order.domain.req.PayCallBackReq;
import com.ai.module.order.domain.req.WxPayOrderReq;
import com.ai.module.order.domain.vo.BusinessServiceVo;
import com.ai.module.order.domain.vo.PayInfoVo;
import com.ai.module.order.domain.vo.WeChatPayStatus;
import com.ai.module.order.en.OrderStatus;
import com.ai.module.order.service.BusinessServiceService;
import com.ai.module.order.service.UserOrderService;
import com.ai.module.order.service.WxOrderService;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wechat.pay.java.service.partnerpayments.app.model.Transaction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: MrHe
 * @create: 2024-02-05 00:10
 * @description: 订单服务
 **/
@RestController
@RequestMapping("/member/order")
@Api(tags = "用户订单")
@Slf4j
public class OrderController {

    @Autowired
    private WxOrderService wxOrderService;

    @Autowired
    private BusinessServiceService businessService;

    @PostMapping("create")
    @ApiOperation(value = "生成用户订单")
    public WxOrderEntity createOrder(@RequestBody OrderReq orderReq) {
        return wxOrderService.createOrder(orderReq);
    }


    @GetMapping("serviceInfo")
    @ApiOperation(value = "获取支付服务")
    @NoLog
    public List<BusinessServiceVo> serviceInfo() {

        return wxOrderService.serviceInfo();
    }


    @PostMapping("wxPay")
    @ApiOperation(value = "用户微信支付")
    public PayInfoVo wxPay(@RequestBody WxPayOrderReq wxPayOrderReq) {
        return wxOrderService.wxPay(wxPayOrderReq);
    }

    // notifyOrder

    @PostMapping("notifyOrder")
    @ApiOperation(value = "微信回调接口")
    @NoAuth
    public Map<String, Object> notifyOrder(@RequestBody PayCallBackReq payCallBack, String orderId) {
        log.info("微信回调接口 payCallBack[{}],orderId [{}]", payCallBack, orderId);
        return wxOrderService.notifyOrder(payCallBack);
    }


    @GetMapping("payStatus")
    @ApiOperation(value = "微信回调接口")
    public WeChatPayStatus payStatus(String orderId) {

        return wxOrderService.payStatus(orderId);
    }

    @PostMapping("testPayCall")
    @ApiOperation(value = "微信回调接口")
    @NoAuth
    public void testPayCall(@RequestBody JSONObject orderIdObject){

         wxOrderService.dealCallBackByDecrypt(orderIdObject.toString());
    }

    @Autowired
    private UserOrderService userOrderService;

    @GetMapping("testPayCall")
    @ApiOperation(value = "微信回调接口")
    @NoAuth
    public void testPayCall(String orderId) {

        WeChatPayStatus weChatPayStatus = new WeChatPayStatus();
        weChatPayStatus.setAmount(10);
        weChatPayStatus.setSuccessTime(DateUtils.getCurrentTime());
        weChatPayStatus.setOrderId(orderId);
        //   进行 wx_order状态更改
        // 修改 数据库订单状态
        weChatPayStatus.setOrderStatus(OrderStatus.SUCCESS);
        userOrderService.saveUserOrder(weChatPayStatus);
    }

}
